//入口文件
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

import moment from 'moment'

import VueRource from 'vue-resource'
Vue.use(VueRource)

import Vuex from 'vuex'
Vue.use(Vuex)
var car = JSON.parse(localStorage.getItem('car') || '[]')

var store = new Vuex.Store({
  state: {//this.$store.state.***
    car: car
  },
  mutations: {//this.$store.commit('methodName',params{})
    addToCar(state, goodsinfo) {
      var flag = false
      state.car.some(item => {
        if (item.id === goodsinfo.id) {
          item.count += parseInt(goodsinfo.count)
          flag = true
          return true
        }
      })
      if (!flag) {
        state.car.push(goodsinfo)
      }

      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updateGoodsInfo(state, goodsinfo) {
      state.car.some(item => {
        if (item.id === goodsinfo.id) {
          item.count = parseInt(goodsinfo.count)
        }
        return true
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    removeFromCar(state, id) {
      state.car.some((item, i) => {
        if (item.id == id) {
          state.car.splice(item, 1)
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    updateGoodsSelected(state, info) {
      store.car.some(item => {
        if (item.id == info.id) {
          item.selected = info.selected
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    }
  },
  getters: {//this.$store.getters.*** 相当于计算属性和过滤器
    getAllCount(state) {
      var c = 0
      state.car.forEach(item => {
        c += item.count
      })

      return c
    },
    getGoodsSelected(state) {
      var o = {}
      state.car.array.forEach(item => {
        o[item.id] = item.selected
      });
      return o
    },
    getGoodsCountAndAmount(state) {
      var o = {
        count: 0,
        amount: 0
      }
      state.car.forEach(item => {
        if (item.selected) {
          o.count += item.count
          o.amount += item.count * item.price
        }
      })
      return o
    }

  }
})
Vue.http.options.root = 'http://vue.studyit.io';
//图片预览插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)
Vue.http.options.emulateJSON = true;

//定义全局的过滤器
Vue.filter("dataFormat", function (dataStr, pattern = "yyyy-MM-DD HH:mm:ss") {
  return moment(dataStr).format(pattern)
})
//导入MUI的样式
import './mui/dist/css/mui.css'
import './mui/dist/css/icons-extra.css'


//按需导入mint-ui的组件
/* import { Header, Swipe, SwipeItem, Button, Lazyload } from 'mint-ui';
Vue.component(Header.name, Header);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);
Vue.use(Lazyload); */
import MintUi from 'mint-ui'
import 'mint-ui/lib/style.css'
Vue.use(MintUi)
//导入路由模块
import router from './router.js'

//导入App组件
import app from './App.vue'

var vm = new Vue({
  el: '#app',
  render: c => c(app),
  router,
  store
})